/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var noticeLoadServlet = "../../servlet/system/NoticeLoad";
var onlineLoadServlet = '../../servlet/data/RealtimeOnlineLoad';
var ysjLoadServlet = '../../servlet/data/YsjLoad';

var now = new Date();
var thisYear = now.getFullYear();
var thisMonth = now.getMonth() + 1;
var noticeStore = new Ext.data.JsonStore({
    url: noticeLoadServlet,
    root: 'data',
    idProperty: 'id',
    totalProperty: 'count',
    autoLoad: true,
    baseParams: {token:token,start: 0, limit: 100},
    fields: ['id', 'title', 'notice', 'note', 'userId', 'status', 'createTime', 'orgCode']

});

var noticePanel = new Ext.grid.GridPanel({
    title: '公告',
    store: noticeStore,
    id: 'notice',
    //frame:true,  
    width: '100%',
    height: 300,
    region: 'north',
    autoExpandColumn: 'title',
    columns: [
        {header: 'ID', dataIndex: 'id', hidden: true},
        {header: '标题', id: 'title', sortable: true, dataIndex: 'title',
            renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                var value = record.get('createTime');
                var now = new Date();
                var aWeekBefore = now.add(Date.DAY, -7);
                var title = record.get('title');

                if (aWeekBefore.format("Y-m-d H:i:s") < value) {
                    return '<img  src="../../image/gif/new.gif"/>' + title;
                }
                else {
                    return title;
                }
            }
        },
        {header: '单位', width: 100, sortable: true, dataIndex: 'shortName'},
        {header: '时间', width: 150, sortable: true, dataIndex: 'createTime', xtype: 'datecolumn', format: 'Y-m-d', hidden: true}
    ]
});



var onlineStore = new Ext.data.JsonStore({
    url: onlineLoadServlet,
    root: 'data',
    idProperty: 'id',
    totalProperty: 'count',
    autoLoad: true,
    baseParams: {token:token,start: 0, limit: 100},
    fields: ['id', 'orgName', 'online', 'total', 'rate']

});


//当前设备在线率
var onlinePanel = new Ext.grid.GridPanel({
    id: 'Online',
    store: onlineStore,
    //frame:true, 
    width: '100%',
    autoExpandColumn: 'OnlineOrgName',
    columns: [
        {header: 'ID', dataIndex: 'id', hidden: true},
        {header: '单位', id: 'OnlineOrgName', sortable: true, dataIndex: 'orgName'},
        {header: '总数', width: 60, sortable: true, dataIndex: 'total'},
        {header: '在线数', width: 70, sortable: true, dataIndex: 'online'},
        {header: '在线率(%)', id: 'OnlineRate', width: 70, sortable: true, renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                var total = record.get('total');
                var online = record.get('online');
                var rate = online / total * 100;
                rate = Number(rate);
                rate = rate.toFixed(3);
                return rate;
            }
        }
    ]
});



//合格率报警
var rateAlarmStore = new Ext.data.JsonStore({
    url: ysjLoadServlet,
    root: 'data',
    idProperty: 'rn',
    totalProperty: 'count',
    autoLoad: true,
    baseParams: {token:token,lock: lock, cmd: 'ysjSubOrgAlarm', reportStatus: 1, yearTo: thisYear, monthTo: thisMonth, start: 0, limit: 100},
    fields: ['rn', 'orgName', 'planRate', 'rateTotal']
});

var rateAlarmPanel = new Ext.grid.GridPanel({
    id: 'RateAlarm',
    store: rateAlarmStore,
    //frame:true, 
    width: '100%',
    region: 'center',
    split: true,
    autoExpandColumn: 'OrgName',
    columns: [
        {header: 'ID', dataIndex: 'rn', hidden: true},
        {header: '单位', id: 'OrgName', sortable: true, dataIndex: 'orgName'},
        {header: '年度指标(%)', sortable: true, dataIndex: 'planRate'},
        {header: '综合合格率(%)', id: 'TotalRate', width: 60, sortable: true, dataIndex: 'rateTotal', css: 'color:red;'}
    ]
});


var gridTabPanel = new Ext.TabPanel({
    width: '100%',
    region: 'center',
    id: 'tab',
    items: [
        {title: '在线率统计', layout: 'fit', items: [onlinePanel]},
        {title: '监测点月合格率报警提醒', layout: 'fit', items: [rateAlarmPanel]}
    ],
    activeTab: 0
});


var westPanel = new Ext.Panel({
    width: '40%',
    layout: 'border',
    region: 'west',
    items: [
        gridTabPanel,
        noticePanel
    ]
});


var rateMonthlyServlet = '../../servlet/data/YsjLoad';

var rateMonthlyStore = new Ext.data.JsonStore({
    url: rateMonthlyServlet,
    root: 'data',
    idProperty: 'rn',
    totalProperty: 'count',
    autoLoad: true,
    fields: ['rn', 'rateTotal'],
    baseParams: {token:token,lock: "Lock", cmd: 'nsjReportOrg', reportStatus: reportStatus, yearTo: thisYear, start: 0, limit: 99999}
});

var tipRendererMonth = function(chart, rec, index, series) {
    var yearInfo = '月份: ' + rec.get('rn');
    var empInfo = '合格率(%)' + ': '
            + rec.get('rateTotal');
    return yearInfo + '\n' + empInfo;
};
var onlineMonthseries = [
    {
        style: {
            lineColor: 0x8B2252,
            lineSize: 3
        }
    }
]
var rateMonthlyPanel = {
    id: 'monthChart',
    layout: 'fit',
    xtype: 'linechart',
    store: rateMonthlyStore,
    xField: 'rn',
    yField: 'rateTotal',
    series: onlineMonthseries,
    tipRenderer: tipRendererMonth,
    xAxis: new Ext.chart.NumericAxis({
        minimum: 0,
        maxmum: 12,
        majorUnit: 1,
        minorUnit: 1,
        displayName: '月',
        title: '月份',
        labelRenderer: Ext.util.Format.
                numberRenderer('0,000')
    }),
    yAxis: new Ext.chart.NumericAxis({
        //maxmum: 105,
        //minimum: 60,
        maxunit: 5,
        displayName: '合格率',
        title: '合格率(%)',
        labelRenderer: Ext.util.Format.
                numberRenderer('0,000.000')
    })
};

var rateDayServlet = '../../servlet/data/RsjLoad';

var rateDayStore = new Ext.data.JsonStore({
    url: rateDayServlet,
    root: 'data',
    idProperty: 'dataDate',
    totalProperty: 'count',
    autoLoad: true,
    fields: ['rn', 'dateStr', 'rateTotal'],
    baseParams: {token:token,lock: lock, cmd: 'rsjReportOrg', reportStatus: reportStatus, dateTo: now.add(Date.DAY, -1).format('Ymd'), start: 0, limit: 99999}
});

var tipRenderer = function(chart, rec, index, series) {
    var yearInfo = '日期: ' + rec.get('dateStr');
    var empInfo = '合格率(%)' + ': '
            + rec.get('rateTotal');
    return yearInfo + '\n' + empInfo;
};

var onlineDayseries = [
    {
        style: {
            lineColor: 0x8B2252,
            lineSize: 3
        },
        markerConfig: {
            type: 'circle',
            size: 6,
            radius: 6,
            'stroke-width': 0            
        }
    }
]

var rateDayPanel = {
    id: 'DayChart',
    tipRenderer: tipRenderer,
    layout: 'fit',
    xtype: 'linechart',
    store: rateDayStore,
    xField: 'rn',
    yField: 'rateTotal',
    series: onlineDayseries,
    xAxis: new Ext.chart.NumericAxis({
        minimum: 0,
        maxmum: 31,
        majorUnit: 1,
        minorUnit: 1,
        displayName: '日',
        title: '日',
        labelRenderer: Ext.util.Format.
                numberRenderer('0')
    }),
    yAxis: new Ext.chart.NumericAxis({
        minimum: 60,
        maxmum: 105,
        //maxunit: 5,
        title: '合格率(%)',
        displayName: '合格率',
        //titleRotation: -90,        
        labelRenderer: Ext.util.Format.
                numberRenderer('0,000.000')
    })
};

var onlineMapseries = [
    {
        yField: 'total',
        displayName: '总数',
        style: {
            fillColor: 0xEE7AE9,
            borderColor: 0xEE7AE9,
            lineColor: 0xEE7AE9,
            lineSize: 2
        }
    }, {
        yField: 'online',
        displayName: '在线数',
        style: {
            fillColor: 0x66CDAA,
            borderColor: 0x66CDAA,
            lineColor: 0x66CDAA,
            lineSize: 2
        }
    }/*,
     {
     yField: 'rate',
     displayName: '在线率',
     style: {
     fillColor: 0x00CCFF,
     borderColor: 0x00CCFF,
     lineColor: 0x00CCFF,
     lineSize: 2
     }
     }*/
];

//在线上传柱状图

var onlineMapStore = new Ext.data.JsonStore({
    url: onlineLoadServlet,
    root: 'data',
    idProperty: 'id',
    totalProperty: 'count',
    autoLoad: true,
    baseParams: {token:token,start: 0, limit: 100},
    fields: ['id', 'orgName', 'online', 'total', 'rate']
});

var tipRendererOnline = function(chart, rec, index, series) {
    var yearInfo = '单位: ' + rec.get('orgName');
    var empInfo = series.displayName + ': '
            + rec.get(series.yField);
    return yearInfo + '\n' + empInfo;
};

var extraStyle = {
    xAxis: {
        majorGridLines: {
            color: 0x999999,
            size: 1
        }
    },
    yAxis: {
        titleRotation: -90
    },
    legend: {
        display: 'bottom',
        padding: 5,
        spacing: 2,
        font: {color: 0x000000, family: 'Arial', size: 12},
        border: {size: 1, color: 0x999999}
    }
};

//在线率柱状图
var onlineMapPanel = {
    layout: 'fit',
    xtype: 'columnchart',
    store: onlineMapStore,
    xField: ['orgName'],
    series: onlineMapseries,
    extraStyle: extraStyle,
    tipRenderer: tipRendererOnline,
    xAxis: new Ext.chart.CategoryAxis({
        displayName: '单位名称',
        title: '单位名称'
    })
};

var pictureTabPanel = new Ext.TabPanel({
    width: '100%',
    height: '100%',
    region: 'center',
    items: [
        //{title: '日合格率走势', layout: 'fit', items: [rateDayPanel]},
        {title: '月合格率走势', layout: 'fit', items: [rateMonthlyPanel]},
        {title: '在线上传柱状图', layout: 'fit', items: [onlineMapPanel]}
    ],
    activeTab: 0
});
//alert(typeof window.parent.isDailyChartShow);
if (window.parent.isDailyChartShow === 'true') {
    pictureTabPanel.insert(0, {title: '日合格率走势', layout: 'fit', items: [rateDayPanel]});
}
var centerPanel = new Ext.Panel({
    width: '60%',
    layout: 'border',
    region: 'center',
    items: [
        pictureTabPanel
    ]
});

function buildWindow() {
    var applicationUI = new Ext.Viewport({
        layout: 'border',
        items: [
            centerPanel, westPanel
        ]
    });
}
Ext.onReady(buildWindow);
 